﻿using System;
using System.Web.Mvc;
using INSE6260.OnlineBanking.Model.Accounts;
using INSE6260.OnlineBanking.Infrastructure;
using INSE6260.OnlineBanking.Model.Payee;
using INSE6260.OnlineBanking.Service.Interfaces;
using INSE6260.OnlineBanking.Service.Messaging.TransactionService;
using INSE6260.OnlineBanking.Web.MVC.Helpers;

namespace INSE6260.OnlineBanking.Web.MVC.Controllers
{
    public class TransactionController : Controller
    {
        private readonly ITransactionManager _transactionService;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IAccountRepository _accountRepository;
        private readonly IPayeeRepository _payeeRepository;
        public TransactionController(ITransactionManager transactionService, IAccountRepository accountRepository,IPayeeRepository payeeRepository, IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _transactionService = transactionService;
            _accountRepository = accountRepository;
            _payeeRepository = payeeRepository;
        }

        #region Withdraw

        public ActionResult Withdraw()
        {
            SetWithdrawDropdowns();
            return View();
        }

        private void SetWithdrawDropdowns()
        {
            ViewBag.SourceAccounts = new SelectList(_accountRepository.GetSourceWithdrawalAccounts(UserProvider.ActiveClient.ClientId), "AccountId","AccountDesc");
            ViewBag.DestinationAccounts = new SelectList(_accountRepository.GetDestinationWithdrawalAccounts(UserProvider.ActiveClient.ClientId),"AccountId", "AccountDesc");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Withdraw(WithdrawRequest withdrawRequest)
        {
            if (ModelState.IsValid)
            {
                TransactionResponse transactionResponse = null;
                try
                {
                    transactionResponse = _transactionService.Withdraw(withdrawRequest);
                    ViewBag.Message = "Transaction was performed successfully.";
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError("", exception.Message);
                    ViewBag.Message = exception.Message;
                    SetWithdrawDropdowns();
                    return View();
                }
                return View("Success", transactionResponse);
            }
            return View();
        }
        #endregion

        #region Transfer
        public ActionResult TransferFund()
        {
            SetTransferDropdowns();
            return View();
        }

        private void SetTransferDropdowns()
        {
            ViewBag.SourceAccounts = new SelectList(_accountRepository.GetSourceWithdrawalAccounts(UserProvider.ActiveClient.ClientId), "AccountId", "AccountDesc");
            ViewBag.DestinationAccounts = new SelectList(_accountRepository.GetSourceWithdrawalAccounts(UserProvider.ActiveClient.ClientId), "AccountId", "AccountDesc");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TransferFund(TransferFundRequest transferFundRequest)
        {
            if (ModelState.IsValid)
            {
                TransactionResponse transactionResponse = null;
                try
                {
                    transactionResponse = _transactionService.Transfer(transferFundRequest);
                    ViewBag.Message = "The following transaction was performed successfully.";
                    
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError("", exception.Message);
                    ViewBag.Message = exception.Message;
                    SetTransferDropdowns();
                    return View();
                }
                return View("Success", transactionResponse);
            }
            return View();

        }

        public ActionResult TransferFundExternal()
        {
            SetTransferDropdowns();
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult TransferFundExternal(TransferFundRequest transferFundRequest)
        {
            if (ModelState.IsValid)
            {
                TransactionResponse transactionResponse = null;
                try
                {
                    transactionResponse = _transactionService.Transfer(transferFundRequest);
                    ViewBag.Message = "Transaction was performed successfully.";
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError("", exception.Message);
                    ViewBag.Message = exception.Message;
                    SetTransferDropdowns();
                    return View();
                }
                return View("Success", transactionResponse);
            }
            return View();

        }
        #endregion
       
        #region Payment

        public ActionResult MakePayment()
        {
            SetMakePaymentDropdowns();
            return View();
        }

        private void SetMakePaymentDropdowns()
        {
            ViewBag.SourceAccounts = new SelectList(_accountRepository.GetSourceWithdrawalAccounts(UserProvider.ActiveClient.ClientId), "AccountId","AccountDesc");
            ViewBag.DestinationAccounts = new SelectList(_payeeRepository.GetClientPayee(UserProvider.ActiveClient.ClientId),"Id", "NickName");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult MakePayment(MakePaymentRequest makePaymentRequest)
        {
            if (ModelState.IsValid)
            {
                TransactionResponse transactionResponse = null;
                try
                {
                    transactionResponse = _transactionService.MakePayment(makePaymentRequest);
                    ViewBag.Message = "Transaction was performed successfully.";
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError("", exception.Message);
                    ViewBag.Message = exception.Message;
                    SetMakePaymentDropdowns();
                    return View();
                }
                return View("Success", transactionResponse);
            }
            return View();

        }
        #endregion

        #region Deposit

        public  ActionResult Deposit()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public  ActionResult Deposit(DepositRequest depositRequest)
        {
            if (ModelState.IsValid)
            {
                TransactionResponse transactionResponse = null;
                try
                {
                    transactionResponse = _transactionService.Deposit(depositRequest);
                    ViewBag.Message = "Transaction was performed successfully";
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError("", exception.Message);
                    ViewBag.Message = exception.Message;
                    return View();
                }
                return View("Success", transactionResponse);
            }
            return View();
        }

        #endregion

    }
}
